using System;
using System.Collections.Generic;
using UnityEngine;

public class Inventory : MonoBehaviour
{
	[Serializable]
	public struct ItemSprite
	{
		public ItemTYPE itemTYPE;

		public Sprite itemIcon;

		public Sprite boostIcon;

		public int boostDuration;

		public int boostEffective;

		public float instantCashFactor;
	}

	public static Inventory instance;

	private List<Item> items;

	private List<GameObject> pool;

	public Action onChange;

	[SerializeField]
	private GameObject popup;

	[SerializeField]
	private GameObject itemSlot;

	[SerializeField]
	private Transform container;

	[SerializeField]
	private ItemSprite[] itemSprite;

	public List<Item> Items
	{
		get
		{
			return items;
		}
		set
		{
			items = value;
			Initialize();
		}
	}

	private void Awake()
	{
		instance = this;
	}

	private void Initialize()
	{
		pool = new List<GameObject>();
		for (int i = 0; i < items.Count; i++)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(itemSlot, container);
			gameObject.GetComponent<ItemSlot>().Init(items[i]);
			pool.Add(gameObject);
		}
	}

	public Sprite GetItemSprite(int boostDuration, int boostEffective)
	{
		ItemSprite itemSprite = Array.Find(this.itemSprite, (ItemSprite target) => target.boostDuration == boostDuration && target.boostEffective == boostEffective);
		return itemSprite.itemIcon;
	}

	public Sprite GetItemSprite(float instantCashFactor)
	{
		ItemSprite itemSprite = Array.Find(this.itemSprite, (ItemSprite target) => target.instantCashFactor == instantCashFactor && target.itemTYPE == ItemTYPE.InstantCash);
		return itemSprite.itemIcon;
	}

	public void Add(Item item)
	{
		Item item2 = null;
		if (item.itemTYPE == ItemTYPE.BoostIncome)
		{
			item2 = items.Find((Item target) => target.boostDuration == item.boostDuration && target.boostEffective == item.boostEffective);
		}
		if (item.itemTYPE == ItemTYPE.InstantCash)
		{
			item2 = items.Find((Item target) => target.instantCash == item.instantCash);
		}
		if (item2 == null)
		{
			items.Add(item);
			GameObject gameObject = pool.Find((GameObject target) => !target.activeSelf);
			if (gameObject != null)
			{
				gameObject.GetComponent<ItemSlot>().Init(items[items.Count - 1]);
				gameObject.transform.SetAsLastSibling();
				gameObject.SetActive(value: true);
			}
			else
			{
				gameObject = UnityEngine.Object.Instantiate(itemSlot, container);
				gameObject.GetComponent<ItemSlot>().Init(items[items.Count - 1]);
			}
		}
		else
		{
			items[items.IndexOf(item2)].itemCount++;
			if (onChange != null)
			{
				onChange();
			}
		}
	}

	public void Remove(Item item)
	{
		items.Remove(item);
	}
}
